﻿var FileUploadItem;
if (FileUploadItem == undefined) {
    FileUploadItem = function(fileID, fileName, stored) {
        this.fileName = fileName;
        this.fileID = fileID;
        this.stored = stored;
    }
}

FileUploadItem.prototype.toString = function() {
    return this.fileID + '&' + this.fileName + '&' + (this.stored ? '1' : '0');
}

var FileUploadCollection;
if (FileUploadCollection == undefined) {

    FileUploadCollection = function() {
    this.collection = new Array();
        this.length = 0;
        this.selectedIndex = -1;
        this.selectedValue = '';
    };

}

FileUploadCollection.prototype.addFile = function(fileID, fileName, stored) {
    var addIndex = this.collection.length + 1;
    for (var i = 0; i < this.collection.length; i++) {
        if (this.collection[i].fileName == fileName) addIndex = -1;
    }
    if (addIndex > 0) {
        this.collection.push(new FileUploadItem(fileID, fileName, stored == undefined ? false : stored));
        this.length = this.collection.length;
    }
    return addIndex;
};
FileUploadCollection.prototype.removeFile = function(fileName) {
    for (var i = 0; i < this.collection.length; i++) {
        if (this.collection[i].fileName == fileName) {
            this.collection.splice(i, 1);

            if (this.selectedIndex == i) {
                if (this.selectedIndex > 0) {
                    this.selectedIndex = this.selectedIndex - 1;
                }
                if (this.collection.length > 0)
                    this.selectedValue = this.collection[this.selectedIndex].fileName;
                else {
                    this.selectedIndex = -1;
                    this.selectedValue = '';
                }
            } else {
                this.setSelected(this.selectedValue);
            }

            break;
        }
    }
    this.length = this.collection.length;
};
FileUploadCollection.prototype.getFile = function(index) {
    if (index >= this.collection.length) throw 'Index out of range';
    return this.collection[index].fileName;
};
FileUploadCollection.prototype.loadArray = function(str, separator, override) {
    var _str = new String(str);
    if (override == undefined) override = true;

    if (override == true) {
        while (this.collection.length > 0) this.collection.pop();
        this.length = this.collection.length;
    }
    if (_str.length > 0) {
        var _separator = '';
        if (separator != undefined)
            _separator = new String(separator);
        if (_separator.length == 0) _separator = '$$';

        //var _exp = new RegExp(_separator, 'gi');
        var items = _str.split(_separator);
        for (var i = 0; i < items.length; i++) {
            var item = items[i].split('&');
            if (item.length == 3) {
                this.addFile(item[0], item[1], item[2]);
            }
        }
        
    }
    if (this.length > 0) {
        this.selectedIndex = 0;
        this.selectedValue = this.collection[this.selectedIndex].fileName;
    }
};
FileUploadCollection.prototype.setSelected = function(value) {
    if (typeof (value) == 'number' && this.length > value) {
        this.selectedIndex = value;
        this.selectedValue = (this.selectedIndex >= 0 && this.selectedIndex < this.collection.length) ? this.collection[this.selectedIndex].fileName : '';
    }
    else if (typeof (value) == 'string') {
        for (var i = 0; i < this.collection.length; i++) {
            if (this.collection[i].fileName == value) {
                this.selectedIndex = i;
                this.selectedValue = this.collection[this.selectedIndex].fileName;
                break;
            }
        }
    }
};
FileUploadCollection.prototype.toString = function(separatorChar) {

    var str = '';
    if (separatorChar == undefined) separatorChar = '$$';

    if (separatorChar != undefined) {

        separatorChar = new String(separatorChar);

        if (this.collection.length == 1) {
            str = this.collection[0].toString();
        }
        else if (this.collection.length > 1) {
            for (var i = 0; i < this.collection.length; i++) {
                str = str.concat(this.collection[i].toString()).concat(separatorChar);
            }
            str = str.slice(0, str.lastIndexOf(separatorChar));
        }
        else {
            str = '';
        }
    }
    else {
        str = this.collection.toString();
    }

    return str;
};